package logic

import (
	"context"
	"fmt"
	"product/model/mysql"

	"product/productrpc/internal/svc"
	"product/productrpc/productrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddLogic {
	return &AddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *AddLogic) Add(in *productrpc.AddRequest) (*productrpc.AddResponse, error) {
	// todo: add your logic here and delete this line
	good := mysql.Good{
		Image:  in.Image,
		Name:   in.Name,
		Price:  int(in.Price),
		Num:    int(in.Num),
		Intro:  in.Intro,
		UserId: int(in.UserId),
	}
	mysql.DB.Debug().Where("name = ?", good.Name).Find(&good)
	if good.ID != 0 {
		return nil, fmt.Errorf("商品已存在")
	}
	mysql.DB.Debug().Create(&good)
	return &productrpc.AddResponse{Id: int64(good.ID)}, nil
}
